﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IMLibrary.Net.Enum;


namespace IMLibrary.BinaryPacket
{
    #region 二进制协议
    /// <summary>
    /// 二进制协议
    /// </summary>
    public class BinaryMsg : BasePacket
    {
        /// <summary>
        /// 基础包长度
        /// </summary>
        public static readonly int HeaderLength = 10;

        /// <summary>
        /// 构造函数（建议不要修改）
        /// </summary>
        public BinaryMsg()
            : base(HeaderLength)
        {

        }

        /// <summary>
        /// 构造函数（建议不要修改）
        /// </summary>
        public BinaryMsg(byte commandType)
            : base(HeaderLength)
        { 
            this.CommandType = commandType;
        }

        /// <summary>
        /// 构造函数（建议不要修改）
        /// </summary>
        /// <param name="data"></param>
        public BinaryMsg(byte[] data)
            : base(data, HeaderLength)
        {

        }

        /// <summary>
        /// 消息是否压缩
        /// </summary>
        public bool Compress
        {
            set
            {
                BitWriteRead.SetBitValue(ref BaseData[0], 1, value);
            }
            get
            {
                return BitWriteRead.GetBitValue(BaseData[0], 1);
            }
        }

        /// <summary>
        ///消息是否加密 
        /// </summary>
        public bool Encrypt
        {
            set
            {
                BitWriteRead.SetBitValue(ref BaseData[0], 2, value);
            }
            get
            {
                return BitWriteRead.GetBitValue(BaseData[0], 2);
            }
        }

        /// <summary>
        ///是否APP客户端 
        /// </summary>
        public bool APPClient
        {
            set
            {
                BitWriteRead.SetBitValue(ref BaseData[0], 3, value);
            }
            get
            {
                return BitWriteRead.GetBitValue(BaseData[0], 3);
            }
        }

        /// <summary>
        ///是否首次登录 
        /// </summary>
        public bool FirstLogin
        {
            set
            {
                BitWriteRead.SetBitValue(ref BaseData[0], 4, value);
            }
            get
            {
                return BitWriteRead.GetBitValue(BaseData[0], 4);
            }
        }  

        public bool ToRoom
        {
            set
            {
                BitWriteRead.SetBitValue(ref BaseData[0], 5, value);
            }
            get
            {
                return BitWriteRead.GetBitValue(BaseData[0], 5);
            }
        }
         
        /// <summary>
        /// 消息是否需要保存为离线消息
        /// </summary>
        public bool IsSave
        {
            set
            {
                BitWriteRead.SetBitValue(ref BaseData[0], 6, value);
            }
            get
            {
                return BitWriteRead.GetBitValue(BaseData[0], 6);
            }
        }



        /// <summary>
        /// 命令类型(1字节)
        /// </summary>
        public byte CommandType
        {
            set
            {
                BaseData[1] = value;
            }
            get
            {
                return BaseData[1];
            }
        } 
         
        /// <summary>
        /// 消息发送者ID
        /// (4字节)
        /// </summary>
        public UInt32 From
        {
            set
            {
                Buffer.BlockCopy(BitConverter.GetBytes(value), 0, BaseData, 2, 4);
            }
            get
            {
                return BitConverter.ToUInt32(BaseData, 2);
            }
        }
  

        /// <summary>
        /// 消息接收者或群ID
        /// (4字节)
        /// </summary>
        public UInt32 To
        {
            set
            {
                Buffer.BlockCopy(BitConverter.GetBytes(value), 0, BaseData, 6, 4);
            }
            get
            {
                return BitConverter.ToUInt32(BaseData, 6);
            }
        }


    }
    #endregion
}
